<template>
  <header class="app-header">
    <div class="container header-content">
      <div class="logo">
        <router-link to="/">
          <h1>MyBlog</h1>
        </router-link>
      </div>
      <div class="nav-menu">
        <router-link to="/" class="nav-link">首页</router-link>
        <template v-if="isLoggedIn">
          <router-link to="/dashboard" class="nav-link">控制台</router-link>
          <el-button type="text" @click="handleLogout" class="nav-link">退出</el-button>
        </template>
        <router-link v-else to="/login" class="nav-link">登录</router-link>
      </div>
    </div>
  </header>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

const isLoggedIn = computed(() => {
  return localStorage.getItem('token')
})

const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('user')
  ElMessage.success('退出登录成功')
  router.push('/')
}
</script>

<style lang="less" scoped>
.app-header {
  background-color: white;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 15px 0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  h1 {
    margin: 0;
    font-size: 24px;
    color: #409eff;
  }
  
  a {
    text-decoration: none;
  }
}

.nav-menu {
  display: flex;
  gap: 20px;
  align-items: center;
}

.nav-link {
  color: #606266;
  text-decoration: none;
  font-size: 16px;
  
  &:hover {
    color: #409eff;
  }
  
  &.router-link-active {
    color: #409eff;
  }
}
</style>

